package oct2013.linkednode;

import java.util.HashSet;

public class LinkedListCycle {
	public boolean hasCycle(ListNode head) {
		ListNode cur1 = head;
		ListNode cur2 = head;
		while (cur2 != null && cur2.next != null) {
			cur1 = cur1.next;
			cur2 = cur2.next.next;
			if (cur1 == cur2)
				return true;
		}
		return false;
	}

	public boolean hasCycleHash(ListNode head) {
		HashSet<ListNode> set = new HashSet<ListNode>();
		ListNode cur = head;
		while (cur != null) {
			if (set.contains(cur)) {
				return true;
			}
			set.add(cur);
			cur = cur.next;
		}
		return false;
	}
}
